package com.ittony.health.controller;

import com.alibaba.druid.sql.visitor.SQLASTOutputVisitorUtils;
import com.alibaba.dubbo.config.annotation.Reference;
import com.ittony.health.constant.MessageConstant;
import com.ittony.health.domain.CheckGroup;
import com.ittony.health.domain.CheckItem;
import com.ittony.health.entity.PageResult;
import com.ittony.health.entity.QueryPageBean;
import com.ittony.health.entity.Result;
import com.ittony.health.service.CheckGroupService;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping("/checkgroup")
@CrossOrigin("*")
public class CheckGroupController {
    @Reference
    private CheckGroupService checkGroupService;

    @RequestMapping("/add/{checkItemIds}")
    public Result add(@RequestBody CheckGroup checkGroup,@PathVariable Integer[] checkItemIds) {
        try{
//            System.out.println(checkGroup);
//            System.out.println(checkItemIds);
            checkGroupService.add(checkGroup,checkItemIds);
        } catch (Exception e){
            return new Result(false, MessageConstant.ADD_CHECKGROUP_FAIL);
        }
        return new Result(true, MessageConstant.ADD_CHECKGROUP_SUCCESS);
    }

    @RequestMapping("/findByPage")
    public PageResult findByPage(@RequestBody QueryPageBean queryPageBean) {
        PageResult pageResult = checkGroupService.findByPage(queryPageBean);
        return pageResult;
    }

    @RequestMapping("/findCheckGroupById")
    public Result findCheckGroupById(@RequestParam Integer id) {
        try {
            CheckGroup checkGroup = checkGroupService.findCheckGroupById(id);
            return new Result(true,MessageConstant.QUERY_CHECKGROUP_SUCCESS,checkGroup);
        } catch (Exception e) {
            return new Result(false, MessageConstant.QUERY_CHECKGROUP_FAIL);
        }

    }
//    @RequestMapping("/findById")
//    public Result findById(@RequestParam Integer id) {
//        try {
//            List<CheckItem> checkItemList = checkGroupService.findById(id);
//            return new Result(true,MessageConstant.QUERY_CHECKGROUP_SUCCESS,checkItemList);
//        } catch (Exception e){
//            return new Result(false,MessageConstant.QUERY_CHECKGROUP_FAIL);
//        }
//    }

    @RequestMapping("/findCheckItemIdsByCheckGroupId/{id}")
    public Result findCheckItemIdsByCheckGroupId(@PathVariable Integer id) {
        try {
             List<Integer> checkItemIds = checkGroupService.findCheckItemIdsByCheckGroupId(id);
             return new Result(true,MessageConstant.QUERY_CHECKITEM_SUCCESS,checkItemIds);
        }catch (Exception e) {
            return new Result(false,MessageConstant.QUERY_CHECKITEM_FAIL);
        }
    }

    @RequestMapping("/findById/{id}")
    public Result findById(@PathVariable Integer id) {
        try{
           CheckGroup checkGroup = checkGroupService.findById(id);
           return new Result(true,MessageConstant.QUERY_CHECKGROUP_SUCCESS,checkGroup);
        }catch (Exception e){
            return new Result(false,MessageConstant.QUERY_CHECKGROUP_FAIL);
        }
    }

    @RequestMapping("/delete/{checkGroupId}")
    public Result delete(@PathVariable Integer checkGroupId) {
        try {
            checkGroupService.delete(checkGroupId);
        }catch (Exception e){
            return new Result(false,MessageConstant.DELETE_CHECKGROUP_FAIL);
        }
        return new Result(true,MessageConstant.DELETE_CHECKGROUP_SUCCESS);
    }

    @RequestMapping("/edit/{checkItemIds}")
    public Result edit(@RequestBody CheckGroup checkGroup,@PathVariable Integer[] checkItemIds) {
        try{
            checkGroupService.edit(checkGroup,checkItemIds);
        }catch (Exception e) {
            return new Result(false,MessageConstant.EDIT_CHECKGROUP_FAIL);
        }
        return new Result(true,MessageConstant.EDIT_CHECKGROUP_SUCCESS);
    }

    @RequestMapping("/findAll")
    public Result findAll() {
        try{
           List<CheckGroup> checkGroupList = checkGroupService.findAll();
           return new Result(true,MessageConstant.QUERY_CHECKGROUP_SUCCESS,checkGroupList);
        }catch(Exception e){
            return new Result(false,MessageConstant.QUERY_CHECKGROUP_FAIL);
        }
    }
}
